package com.ssd.controller;

import com.ssd.entity.NodeEntity;
import com.ssd.entity.NodeEntity;
import com.ssd.service.NodeService;
import com.ssd.utils.StringUtils;
import com.ssd.view.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/node")
@Slf4j
public class NodeController {

    @Resource
    private NodeService nodeService;

    // 分页查询商品列表
    @GetMapping("/list")
    public BaseResponse<Map<String, Object>> getNodePage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10000") int pageSize,
            @RequestParam(required = false) Long regionId,
            @RequestParam(required = false) String name) {
        return new BaseResponse(nodeService.getNodeList(pageNum, pageSize,regionId,name));
    }

    @GetMapping("/{nodeId}")
    public BaseResponse<NodeEntity> getNodeDetail(@PathVariable("nodeId") Long nodeId) {
        return new BaseResponse(nodeService.selectNodeById(nodeId));
    }

    @PostMapping
    public BaseResponse<String> addNode(@RequestBody NodeEntity node) {
        return new BaseResponse(nodeService.insertNode(node));
    }
 
    @PutMapping
    public BaseResponse<String> updateNode(@RequestBody NodeEntity node) {
        return new BaseResponse(nodeService.updateNode(node));
    }

    @DeleteMapping("/{nodeIds}")
    public BaseResponse<String> batchDeleteNode(@PathVariable("nodeIds") String classIdsStr) {
        Long[] classIds = StringUtils.isEmpty(classIdsStr) ?
                new Long[0] :
                Arrays.stream(classIdsStr.split(","))
                        .map(Long::parseLong)
                        .toArray(Long[]::new);
        return new BaseResponse(nodeService.deleteNodeByNodeIds(classIds));
    }
}